
/**
 * 记录每帧的关键数据
 */
export class CameraAnimationFrame implements IAnimationFrame {
    setTarget(ansT: THREE.Vector3) {
        this.target.copy(ansT);
    }
    setPosition(ansP: THREE.Vector3) {
        this.position.copy(ansP);
    }
    private name: string = "";
    private position: THREE.Vector3 = new THREE.Vector3();
    private target: THREE.Vector3 = new THREE.Vector3();
    private msTimeAnchor: number = 0;
    private distance2go: number = 0;

    setDistance2go(dis: number) {
        this.distance2go = dis;
    }
    getDistance2Go(): number {
        return this.distance2go;
    }
    getMsTimeAnchor(): number {
        return this.msTimeAnchor;
    }
    setData(name: string, view: any, timeAnchorMs?: number): CameraAnimationFrame {
        this.name = name;
        this.position = new THREE.Vector3().fromArray(view.viewport.eye);
        this.target = new THREE.Vector3().fromArray(view.viewport.target);
        if (timeAnchorMs !== undefined) {
            this.msTimeAnchor = timeAnchorMs;
        }
        return this;
    }
    getPosition(): THREE.Vector3 {
        return this.position.clone();
    }
    getTarget(): THREE.Vector3 {
        return this.target.clone();
    }
    setMsTimeAnchor(ms: number) {
        this.msTimeAnchor = ms;
    }
    calcMsTimeAnchor(preTimeAnchor: number, preDis: number, disPerMs: number) {
        this.msTimeAnchor = preTimeAnchor + (preDis / disPerMs);
    }
    /**
     * 根据前一帧后一帧和局部进度生成一插帧
     * @param p1
     * @param p2
     * @param localProcess
     * @returns
     */
    setInsertionFrameByLoaclprocess(p1: CameraAnimationFrame, p2: CameraAnimationFrame, localProcess: number): CameraAnimationFrame {
        this.setPosition(p1.getPosition().add(p2.getPosition().sub(p1.getPosition()).multiplyScalar(localProcess)));
        this.setTarget(p1.getTarget().add(p2.getTarget().sub(p1.getTarget()).multiplyScalar(localProcess)));
        return this;
    }
    setInsertionFrameByAnimationTime(p1: CameraAnimationFrame, p2: CameraAnimationFrame, animTime: number): CameraAnimationFrame {
        let localProcess = 0;
        let allTime = p2.getMsTimeAnchor() - p1.getMsTimeAnchor();
        let partTime = animTime - p1.getMsTimeAnchor();
        if (allTime == 0) {
            localProcess = 1
        } else {
            localProcess = partTime / allTime;
        }
        this.setPosition(p1.getPosition().add(p2.getPosition().sub(p1.getPosition()).multiplyScalar(localProcess)));
        this.setTarget(p1.getTarget().add(p2.getTarget().sub(p1.getTarget()).multiplyScalar(localProcess)));
        return this;
    }
}
interface IAnimationFrame {
    getPosition(): THREE.Vector3;
    getTarget(): THREE.Vector3;
    setTarget(v: THREE.Vector3): void;
    setPosition(v: THREE.Vector3): void;
}

